﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using ServBus.ListForm2013.Setup.Utilities;

namespace ServBus.ListForm2013.Setup.Validators
{
    public class SolutionValidator : BaseValidator
    {
        public SolutionStatus Status { get; private set; }

        public override string CheckItemText
        {
            get { return Properties.Resources.CheckingSolution; }
        }

        public override string ValidText
        {
            get 
            {
                switch (this.Status)
                {
                    case SolutionStatus.None:
                        return Properties.Resources.CheckNoneSolution;
                    case SolutionStatus.Older:
                        return Properties.Resources.CheckOldSolution;
                    default:
                        return Properties.Resources.CheckSameSolution;
                }
            }
        }

        public override string InvalidText
        {
            get { return Properties.Resources.CheckFailSolution; }
        }

        public override void Validate()
        {
            this.Validated = true;
            SPSolution solution = SPFarm.Local.Solutions[Constants.WSPNAME];
            if (solution == null)
            {
                this.Status = SolutionStatus.None;
            }
            else
            {
                double currentVersion;
                if (solution.Properties.Contains(Constants.VERSION_STRING) &&
                    double.TryParse(Convert.ToString(solution.Properties[Constants.VERSION_STRING]), out currentVersion))
                {
                    if (currentVersion == Constants.VERSION)
                        this.Status = SolutionStatus.Same;
                    else if (currentVersion < Constants.VERSION)
                        this.Status = SolutionStatus.Older;
                    else
                        this.Validated = false;
                }
                else
                {
                    this.Status = SolutionStatus.Older;
                }
            }
        }
    }
}
